<?php
declare(strict_types=1);

namespace app\controller\api\admin\general;

use think\App;
use think\Request;
use think\exception\ValidateException;

use app\controller\api\ApiBaseController;
use app\logic\api\admin\general\RequestLogLogic;
// use app\validate\api\admin\general\RequestLogValidate;

class RequestLogController extends ApiBaseController
{
    protected $logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->logic = new RequestLogLogic();
    }

    public function index(Request $request)
    {
        $param = $request->param();

        $where = [];
        isset($param['user_id']) && $param['user_id'] != '' && $where[] = ['user_id', '=', $param['user_id']];
        isset($param['user_name']) && $param['user_name'] != '' && $where[] = ['user_name', 'like', '%' . $param['user_name'] . '%'];
        isset($param['from']) && $param['from'] != '' && $where[] = ['from', '=', $param['from']];
        isset($param['content']) && $param['content'] != '' && $where[] = ['content', 'like', '%' . $param['content'] . '%'];
        isset($param['method']) && $param['method'] != '' && $where[] = ['method', '=', $param['method']];
        isset($param['url']) && $param['url'] != '' && $where[] = ['url', 'like', '%' . $param['url'] . '%'];
        isset($param['route']) && $param['route'] != '' && $where[] = ['route', 'like', '%' . $param['route'] . '%'];
        isset($param['ip']) && $param['ip'] != '' && $where[] = ['ip', 'like', '%' . $param['ip'] . '%'];
        isset($param['create_time']) && $param['create_time'] != '' && $where[] = ['create_time', 'between time', [$param['create_time'][0], $param['create_time'][1]]];

        $lgc_re = $this->logic->list($where, '*', ['id' => 'desc']);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }
}
